namespace Domotica.Windows.Input
{
    using System;
    using System.ComponentModel;
    using System.Globalization;
    using System.Runtime.CompilerServices;
    using System.Windows.Input;

    public class SignGestureConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return (sourceType == typeof(string));
        }

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (((((destinationType == typeof(string)) && (context != null)) && (context.Instance != null)) ? 1 : 0) != 0)
            {
                SignGesture instance = context.Instance as SignGesture;
                if (instance != null)
                {
                    return (ModifierKeysConverter.IsDefinedModifierKeys(instance.Modifiers) && SignActionConverter.IsDefinedSignAction(instance.SignAction));
                }
            }
            return false;
        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object source)
        {
            if ((((source is string) && (source != null)) ? 1 : 0) != 0)
            {
                string str2;
                string str3;
                string str = ((string)(source)).Trim();
                if (str == string.Empty)
                {
                    return new SignGesture(SignAction.None, ModifierKeys.None);
                }
                int length = str.LastIndexOf('+');
                if (length >= 0)
                {
                    str2 = str.Substring(0, length);
                    str3 = str.Substring(length + 1);
                }
                else
                {
                    str2 = string.Empty;
                    str3 = str;
                }
                TypeConverter converter2 = TypeDescriptor.GetConverter(typeof(SignAction));
                if (converter2 != null)
                {
                    object obj3 = RuntimeHelpers.GetObjectValue(converter2.ConvertFrom(context, culture, str3));
                    if (obj3 != null)
                    {
                        if (str2 != string.Empty)
                        {
                            TypeConverter converter = TypeDescriptor.GetConverter(typeof(ModifierKeys));
                            if (converter != null)
                            {
                                object obj2 = RuntimeHelpers.GetObjectValue(converter.ConvertFrom(context, culture, str2));
                                if ((((obj2 != null) && (obj2 is ModifierKeys)) ? 1 : 0) != 0)
                                {
                                    return new SignGesture((SignAction) obj3, (ModifierKeys) obj2);
                                }
                            }
                        }
                        else
                        {
                            return new SignGesture((SignAction) obj3);
                        }
                    }
                }
            }
            throw base.GetConvertFromException(RuntimeHelpers.GetObjectValue(source));
        }

        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == null)
            {
                throw new ArgumentNullException("destinationType");
            }
            if (destinationType == typeof(string))
            {
                if (value == null)
                {
                    return string.Empty;
                }
                SignGesture gesture = value as SignGesture;
                if (gesture != null)
                {
                    string str = "";
                    TypeConverter converter2 = TypeDescriptor.GetConverter(typeof(ModifierKeys));
                    if (converter2 != null)
                    {
                        str = str + (converter2.ConvertTo(context, culture, gesture.Modifiers, destinationType) as string);
                        if (str != string.Empty)
                        {
                            str = str + "+";
                        }
                    }
                    TypeConverter converter = TypeDescriptor.GetConverter(typeof(SignAction));
                    if (converter != null)
                    {
                        str = str + (converter.ConvertTo(context, culture, gesture.SignAction, destinationType) as string);
                    }
                    return str;
                }
            }
            throw base.GetConvertToException(RuntimeHelpers.GetObjectValue(value), destinationType);
        }
    }
}

